package com.reger.core.model;

import com.reger.core.exception.ExceptionStatus;
import com.reger.core.exception.GlobalException;
import com.reger.core.exception.GlobalExceptionStatus;
import com.reger.core.exception.Level;
import com.reger.core.utils.ContextUtils;

public class ResponseEntity<T> implements Entity<T> {

	private static final long serialVersionUID = 1L;
	private long curtime = System.currentTimeMillis(); // 返回时间戳
	private String message;
	private String details;
	private Level level;
	private Integer code;// 返回 错误代码
	private String token; // 返回token
	private T entity;// 响应中的数据

	public ResponseEntity() {
		this(null, null);
	}

	public ResponseEntity(T entity) {
		this(entity, null);
	}

	public ResponseEntity(ExceptionStatus ExceptionStatus) {
		this(null, ExceptionStatus);
	}

	public ResponseEntity(GlobalException customException) {
		this.code = customException.getCode();
		this.message = customException.getDescribe();
		this.details = customException.getDetails();
		this.level = customException.getLevel();
	}

	public ResponseEntity(T entity, ExceptionStatus exceptionStatus) {
		this.entity = entity;
		if (exceptionStatus == null)
			exceptionStatus = GlobalExceptionStatus.OK;
		this.code = exceptionStatus.getCode();
		this.message = exceptionStatus.getMessage();
		this.level = exceptionStatus.getLevel();
	}

	public static <S> ResponseEntity<S> success() {
		return new ResponseEntity<>();
	}

	public static <S> ResponseEntity<S> success(S entity) {
		return new ResponseEntity<>(entity);
	}

	public static <S> ResponseEntity<S> fail(String message) {
		String details = null;
		return fail(message, details);
	}

	public static <S> ResponseEntity<S> fail(String message, String details) {
		return fail(GlobalExceptionStatus.WARN, message, details);
	}

	public static <S> ResponseEntity<S> fail(GlobalException customException) {
		return new ResponseEntity<>(customException);
	}

	public static <S> ResponseEntity<S> fail(S entity, ExceptionStatus ExceptionStatus) {
		return new ResponseEntity<>(entity, ExceptionStatus);
	}

	public static <S> ResponseEntity<S> fail(ExceptionStatus ExceptionStatus) {
		return new ResponseEntity<>(ExceptionStatus);
	}

	public static <S> ResponseEntity<S> fail(ExceptionStatus ExceptionStatus, String message, String details) {
		ResponseEntity<S> entity = new ResponseEntity<>(ExceptionStatus);
		entity.setDetails(details);
		entity.setMessage(message);
		return entity;
	}

	@Override
	public boolean isSucceed() {
		return 200 == code || 0 == code;
	}

	public String getToken() {
		ContextUtils.setToken(this);
		return token;
	}

	@Override
	public String getMessage() {
		if (details == null || "".equals(details) || details.startsWith("{") || details.startsWith("[")) {
			return message;
		}
		return this.details;
	}

	@Override
	public String getDetails() {
		return details;
	}

	@Override
	public T getEntity() {
		return entity;
	}

	@Override
	public Level getLevel() {
		return level;
	}

	@Override
	public int getCode() {
		return this.code;
	}

	public long getCurtime() {
		return curtime;
	}

	public void setCurtime(long curtime) {
		this.curtime = curtime;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public void setLevel(Level level) {
		this.level = level;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}

}
